import { getVCode } from '@/apis'
import { LoginDto } from '@/apis/auth/type/dto'
import { catchToTuple } from '@/utils'

interface UseCaptcha {
  formData: LoginDto
}

export const useCaptcha = ({ formData }: UseCaptcha) => {
  const vCodeLoading = ref<boolean>(true)
  const captcha = ref<string>('')

  const getCaptcha = async (): Promise<void> => {
    vCodeLoading.value = true
    const [err, data] = await catchToTuple(getVCode())

    if (err) {
      vCodeLoading.value = false
      return
    }

    if (data?.code === 200 && data.data) {
      formData.uuid = data.data.captchaId
      captcha.value = data.data.svg
    }
    vCodeLoading.value = false
  }

  return {
    vCodeLoading,
    captcha,
    getCaptcha,
  }
}
